package mysql

import "gorm.io/gorm"

type Goods struct {
	gorm.Model
	GoodsName      string `gorm:"type:varchar(100)"`  //商品名称
	GoodsInventory int64  `gorm:"index,type:int(10)"` //商品数量
	GoodsPrice     float64
	GoodsViews     int64 `gorm:"index,type:int(10)"` //商品浏览量
}

// 商品属性表
type Property struct {
	gorm.Model
	GoodsId int64  `gorm:"index,type:int(10)"` //商品id
	Name    string `gorm:"type:varchar(100)"`  //商品属性名
}

// 商品属性
type PropertyValue struct {
	gorm.Model
	PropertyId int64  `gorm:"index,type:int(10)"` //属性id
	Value      string `gorm:"type:varchar(100)"`  //属性值
}

// 商品sku
type Sku struct {
	gorm.Model
	GoodsId     int64  `gorm:"index,type:int(10)"` //商品id
	ProValIdFir int64  `gorm:"index,type:int(10)"` //第一个商品属性值
	ProValIdSec int64  `gorm:"index,type:int(10)"` //第二个商品属性值
	ProValIdThi int64  `gorm:"index,type:int(10)"` //第三个商品属性值
	Price       string `gorm:"type:decimal(10,2)"` //单价
	Stock       int64  `gorm:"index,type:int(6)"`  //库存
}

type Comments struct {
	gorm.Model
	GoodsId int64  //商品ID
	UserID  int64  //品论的用户ID
	Comment string //评论
}

type ShoppingCart struct {
	gorm.Model
	UserID         int64   //品论的用户ID
	GoodsId        int64   //商品ID
	GoodsName      string  //商品名称
	GoodsPrice     float64 //商品单价
	GoodsInventory int64   //商品数量
}

type SearchForRecords struct {
	gorm.Model
	UserID           int64  //品论的用户ID
	SearchForRecords string //搜索记录
	Num              int64
}

func CreateComments(comments Comments) error {
	var err = db.Model(&Comments{}).Create(&comments).Error
	return err
}

func Where(goodsid int) []Comments {
	var g []Comments
	db.Model(&Comments{}).Where("goodsid = ?", goodsid).First(&g)
	return g
}

func GoodsViews() ([]Goods, error) {
	var goods []Goods
	err := db.Model(&Goods{}).Order("goods_views").Limit(10).Find(&goods).Error
	return goods, err
}

func WhereAll() []Goods {
	var g []Goods
	db.Model(&Goods{}).Find(&g)
	return g
}

func WhereForRecords(userid int) SearchForRecords {
	var g SearchForRecords
	db.Model(&SearchForRecords{}).Where("user_id = ?", userid).First(&g)
	return g
}

func WhereForRecordsTop(userid int) []SearchForRecords {
	var g []SearchForRecords
	db.Model(&SearchForRecords{}).Where("user_id = ?", userid).Order("num desc").Limit(10).Find(&g)
	return g
}

func CreateForRecords(search SearchForRecords) error {
	err := db.Model(&SearchForRecords{}).Where("user_id = ?", search.UserID).Save(&search).Error
	return err
}

func WhereGoods(id int) Goods {
	var g Goods
	db.Model(&Goods{}).Where("id = ?", id).First(&g)
	return g
}

func AddShoppingCart(shoppingCart ShoppingCart) error {
	var err = db.Model(&ShoppingCart{}).Create(&shoppingCart).Error
	return err
}
